/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vce.web.election;

import com.vce.election.domain.Counting;
import com.vce.election.domain.Jrv;
import com.vce.election.domain.Padron;
import com.vce.web.SearchManagedBean;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Ggutierrez
 */
public class SearchElectorsBean extends SearchManagedBean {

    private String cedula;
    private String codeJrv;
    private List<Padron> electors = new ArrayList<Padron>();
    private List<Object[]> resultsParalelo = new ArrayList<Object[]>();
    private List<Object[]> resultsOficial = new ArrayList<Object[]>();

    public SearchElectorsBean() {
    }

    public String execute() {
        electors = new ArrayList<Padron>();
        setResults(electors);
        resultsParalelo = new ArrayList<Object[]>();
        resultsOficial = new ArrayList<Object[]>();
        if (codeJrv.length() == 0 && cedula.length() > 0) {
            if (cedula.length() != 14) {
                addErrorMessage("Cédula inválida");
                return "fail";
            }

            electors = getElection().findElectors(cedula);

            if (electors == null || electors.size() == 0) {
                addErrorMessage("Cédula no registrada en el padrón electoral");
                return "fail";
            }

            setResults(electors);
            return startup();
        } else if (codeJrv.length() > 0) {
            return startup();
        }
        return "success";
    }

    @Override
    public String startup() {
        if (codeJrv != null && codeJrv.length() > 0) {
            electors = new ArrayList<Padron>();
            setResults(electors);
            cedula = null;

            Jrv jrv = null;
            if (codeJrv.length() > 0) {
                if (codeJrv.length() == 4) {
                    codeJrv = "605".concat(codeJrv);
                }
                List<Jrv> jrvs = getElection().findJrvs(codeJrv);
                
                if (jrvs.size() > 0){
                    jrv = jrvs.get(0);
                }
            }
            if (jrv != null) {
                try {
                    resultsParalelo = getElection().findResults(jrv, Counting.PARALELO);
                } catch (Exception e) {
                    resultsParalelo = new ArrayList<Object[]>();
                    e.printStackTrace();
                }

                try {
                    resultsOficial = getElection().findResults(jrv, Counting.OFICIAL);
                } catch (Exception e) {
                    resultsOficial = new ArrayList<Object[]>();
                    e.printStackTrace();
                }
            } else {
                addErrorMessage("JRV inválida");
                return "fail";
            }
        } else if (electors != null && electors.size() > 0) {
            Jrv jrv = electors.get(0).getJrv();
            try {
                resultsParalelo = getElection().findResults(jrv, Counting.PARALELO);
            } catch (Exception e) {
                addErrorMessage("No existen datos en nuestros registros");
                resultsParalelo = new ArrayList<Object[]>();
                e.printStackTrace();
            }

            try {
                resultsOficial = getElection().findResults(jrv, Counting.OFICIAL);
            } catch (Exception e) {
                resultsOficial = new ArrayList<Object[]>();
                e.printStackTrace();
            }
        }

        if(resultsParalelo == null){
            resultsParalelo = new ArrayList<Object[]>();
        }
        
        if(resultsOficial == null){
            resultsOficial = new ArrayList<Object[]>();
        }
        
        if (resultsParalelo.size() == 0) {
            addErrorMessage("No existen datos en nuestros registros");
            return "fail";
        }

        if (resultsOficial.size() == 0) {
            addErrorMessage("JRV no reportada por el CSE. ¡Su voto no está siendo contado!");
            return "fail";
        }
        
        if (resultsOficial.size() == 0 && resultsOficial.size() == 0) {
            addErrorMessage("Este dato no existe en el CSE, ni en nuestros registros.");
            return "fail";
        }

        return "success";
    }

    @Override
    public void reset() {
        cedula = null;
        electors = new ArrayList<Padron>();
        resultsParalelo = new ArrayList<Object[]>();
        resultsOficial = new ArrayList<Object[]>();
        codeJrv = null;
        super.reset();
    }

    public String getCedula() {
        return cedula;
    }

    public void setCedula(String cedula) {
        this.cedula = cedula;
    }

    public List<Object[]> getResultsParalelo() {
        return resultsParalelo;
    }

    public void setResultsParalelo(List<Object[]> resultsParalelo) {
        this.resultsParalelo = resultsParalelo;
    }

    public List<Object[]> getResultsOficial() {
        return resultsOficial;
    }

    public void setResultsOficial(List<Object[]> resultsOficial) {
        this.resultsOficial = resultsOficial;
    }

    public String getCodeJrv() {
        return codeJrv;
    }

    public void setCodeJrv(String codeJrv) {
        this.codeJrv = codeJrv;
    }
}
